package cn.gmph.blog.service.post.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import cn.gmph.blog.service.post.dao.helper.CommentsProvider;
import cn.gmph.blog.service.post.entity.CommentsDO;
import cn.gmph.blog.web.dto.CommentQueryDTO;
import cn.gmph.blog.web.dto.QueryDTO;;

public interface CommentsDAO {

    public CommentsDO selectByPrimaryKey(QueryDTO dto);

    public List<Long> selectLastComments(QueryDTO dto);

    @Results({ @Result(column = "comment_id", property = "commentId"), @Result(column = "comment_post_id", property = "commentPostId"), @Result(column = "comment_author", property = "commentAuthor"),
               @Result(column = "comment_author_email", property = "commentAuthorEmail"), @Result(column = "comment_author_url", property = "commentAuthorUrl"),
               @Result(column = "comment_author_ip", property = "commentAuthorIp"), @Result(column = "comment_date", property = "commentDate"),
               @Result(column = "comment_date_gmt", property = "commentDateGmt"), @Result(column = "comment_content", property = "commentContent"),
               @Result(column = "comment_karma", property = "commentKarma"), @Result(column = "comment_approved", property = "commentApproved"),
               @Result(column = "comment_agent", property = "commentAgent"), @Result(column = "comment_type", property = "commentType"), @Result(column = "comment_parent", property = "commentParent"),
               @Result(column = "user_id", property = "userId") })
    @Select("SELECT * FROM wp_comments WHERE id in #{ids}")

    public List<CommentsDO> selectByIds(@Param("ids") String ids);

    @Select("SELECT t.comment_ID FROM wp_comments t JOIN wp_posts tt ON tt.ID = t.comment_post_ID WHERE t.comment_approved = '1' AND tt.post_status IN ('publish') ORDER BY t.comment_date_gmt DESC LIMIT 5")
    public List<Long> selectAllLastComments(QueryDTO dto);
    
    @SelectProvider(type = CommentsProvider.class, method = "selectForPage")
    public List<Long> selectForPage(@Param("comments") CommentQueryDTO dto);

    @Results({ @Result(column = "comment_id", property = "commentId"), @Result(column = "comment_post_id", property = "commentPostId"), @Result(column = "comment_author", property = "commentAuthor"),
               @Result(column = "comment_author_email", property = "commentAuthorEmail"), @Result(column = "comment_author_url", property = "commentAuthorUrl"),
               @Result(column = "comment_author_ip", property = "commentAuthorIp"), @Result(column = "comment_date", property = "commentDate"),
               @Result(column = "comment_date_gmt", property = "commentDateGmt"), @Result(column = "comment_content", property = "commentContent"),
               @Result(column = "comment_karma", property = "commentKarma"), @Result(column = "comment_approved", property = "commentApproved"),
               @Result(column = "comment_agent", property = "commentAgent"), @Result(column = "comment_type", property = "commentType"), @Result(column = "comment_parent", property = "commentParent"),
               @Result(column = "user_id", property = "userId") })
    @Select("SELECT * FROM wp_comments WHERE id = #{id}")
    public CommentsDO getById(@Param("id") int id);

    @Insert("INSERT into wp_comments(comment_id,comment_post_id,comment_author,comment_author_email,comment_author_url,comment_author_ip,comment_date,comment_date_gmt,comment_content,comment_karma,comment_approved,comment_agent,comment_type,comment_parent,user_id) VALUES (#{commentId},#{commentPostId},#{commentAuthor},#{commentAuthorEmail},#{commentAuthorUrl},#{commentAuthorIp},#{commentDate},#{commentDateGmt},#{commentContent},#{commentKarma},#{commentApproved},#{commentAgent},#{commentType},#{commentParent},#{userId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insert(CommentsDO comments);

    @Delete("DELETE FROM wp_comments WHERE id = #{id}")
    public int deleteById(@Param("id") int id);

    @UpdateProvider(type = CommentsProvider.class, method = "update")
    public int update(@Param("comments") CommentsDO comments);

    @Results({ @Result(column = "comment_id", property = "commentId"), @Result(column = "comment_post_id", property = "commentPostId"), @Result(column = "comment_author", property = "commentAuthor"),
               @Result(column = "comment_author_email", property = "commentAuthorEmail"), @Result(column = "comment_author_url", property = "commentAuthorUrl"),
               @Result(column = "comment_author_ip", property = "commentAuthorIp"), @Result(column = "comment_date", property = "commentDate"),
               @Result(column = "comment_date_gmt", property = "commentDateGmt"), @Result(column = "comment_content", property = "commentContent"),
               @Result(column = "comment_karma", property = "commentKarma"), @Result(column = "comment_approved", property = "commentApproved"),
               @Result(column = "comment_agent", property = "commentAgent"), @Result(column = "comment_type", property = "commentType"), @Result(column = "comment_parent", property = "commentParent"),
               @Result(column = "user_id", property = "userId") })
    @SelectProvider(type = CommentsProvider.class, method = "pageList")
    public List<CommentsDO> pageList(@Param("comments") CommentsDO comments, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @SelectProvider(type = CommentsProvider.class, method = "pageListCount")
    public Integer pageListCount(@Param("comments") CommentsDO comments);

}